/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simpSync;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import simpSync.model.Link;
import simpSync.model.Media;
import simpSync.model.Region;
import simpSync.model.SimpleSync;

/**
 * Classe que converte um documento XML em um objeto SimpSync
 */
public class SimpSyncParser {

    public SimpleSync parse(String xmlFilePath) {
        File xmlFile;
        DocumentBuilderFactory dbf;
        DocumentBuilder db;
        Document doc = null;

        try {
            xmlFile = new File(xmlFilePath);
            dbf = DocumentBuilderFactory.newInstance();
            db = dbf.newDocumentBuilder();
            doc = db.parse(xmlFile);
        } catch (SAXException ex) {
            System.out.println("Erro de conversão do XML: " + ex.getLocalizedMessage());
        } catch (ParserConfigurationException ex) {
            System.out.println("Erro ao criar um novo parser de documento XML: " + ex.getLocalizedMessage());
        } catch (IOException ex) {
            System.out.println("Erro converter o arquivo XML em objeto: " + ex.getLocalizedMessage());
        }

        SimpleSync ss = new SimpleSync();
        Element root = doc.getDocumentElement();

        //Obtendo regions
        NodeList regions = root.getElementsByTagName("region");
        for (int i = 0; i < regions.getLength(); i++) {

            Element node = (Element) regions.item(i);


            Region r = new Region();
            r.setId(node.getAttribute("id"));
            r.setHeight(node.getAttribute("height"));
            r.setWidth(node.getAttribute("width"));
            r.setLeft(node.getAttribute("left"));
            r.setTop(node.getAttribute("top"));
            r.setzIndex(node.getAttribute("zindex"));
            r.setBgcolor(node.getAttribute("bgcolor"));

            ss.getRegions().add(r);
        }

        //Obtendo Medias
        NodeList medias = root.getElementsByTagName("media");
        for (int i = 0; i < medias.getLength(); i++) {
            Element node = (Element) medias.item(i);
            
            Media m = new Media();
            m.setId(node.getAttribute("id"));
            m.setSrc(node.getAttribute("src"));
            m.setType(node.getAttribute("type"));
            m.setDuration(node.getAttribute("duration"));
            m.setRegion(node.getAttribute("region"));
            
            //Tenta pegar o anchor
            NodeList anchors = node.getElementsByTagName("anchor");
            if (anchors.getLength()>0)
            {
                 Element nodeAnchor = (Element) anchors.item(0);
                 m.setStartTime(Integer.parseInt(nodeAnchor.getAttribute("begin")));
                 m.setEndTime(Integer.parseInt(nodeAnchor.getAttribute("end")));
            }
            
            
            ss.getMedias().add(m);
            
        }


        //Obtendo Links
        NodeList links = root.getElementsByTagName("link");

        for (int i = 0; i < links.getLength(); i++) {
            Element node = (Element) links.item(i);
            
            Link l = new Link();
            l.setCondition(node.getAttribute("condition"));
            l.setAction(node.getAttribute("action"));
            l.setSource(node.getAttribute("source"));
            l.setTarget(node.getAttribute("target"));
            l.setWidth(node.getAttribute("width"));
            l.setHeight(node.getAttribute("height"));
            l.setKey(node.getAttribute("key"));
            
            ss.getLinks().add(l);
        }

        return ss;

    }
}
